<template>
  <div class="login-contain">
    <div class="login-box">
      <div class="avater-box">
        <img src="../../assets/logo.png" alt="">
      </div>
      <!-- 登录表单区 -->
      <div class="login-form">
        <el-form label-width="0px" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" prefix-icon="iconfont icon-3702mima"></el-input>
          </el-form-item>
        </el-form>
        <!-- 登录按钮 -->
        <div class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="infor" @click="loginFormReset">重置</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    loginFormReset () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(valid => {
        if (valid) {
          this.$http.post('login', this.loginForm).then(res => {
            if (res.data.meta.status !== 200) {
              this.$message.error(res.data.meta.msg)
            }
            window.sessionStorage.setItem('token', res.data.data.token)
            this.$message.success(res.data.meta.msg)
            this.$router.push('/home')
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .login-contain {
    background-color: #2b4b6b;
    height: 100%;
  }

  .login-box {
    background-color: #fff;
    height: 300px;
    width: 450px;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avater-box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      box-shadow: 0 0 10px #ddd;
      background-color: #eee;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);

      img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
      }
    }

    .login-form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 20px;
      box-sizing: border-box;
    }

    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
</style>
